package com.py.credit.service.impl;

import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.OfficeDao;
import com.py.credit.service.LhjcRemoteBaseService;
import com.py.credit.service.OfficeService;
import com.py.framework.core.entity.SysAreaEntity;
import com.py.framework.core.entity.SysOfficeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class OfficeServiceImpl implements OfficeService {

    @Autowired
    private OfficeDao officeDao;

    @Autowired
    private LhjcRemoteBaseService remoteBaseService;

    /**
     * 查询顶级部门id
     *
     * @param deptId
     * @return
     */
    @Override
    public String selectTopDeptId(String deptId) {
        String topDeptId = officeDao.selectTopDeptId(deptId);
        if (null == topDeptId || topDeptId.equals("")) {
            return deptId;
        } else {
            return topDeptId.split(",")[0];
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncArea() {
        // 远程获取所有区域信息
        RestServiceResult<List<SysAreaEntity>> areaResult = remoteBaseService.getAllAreas(null, null);
        if (null != areaResult && RestServiceResult.CODE_SUCCESS == areaResult.getCode()
                && !CollectionUtils.isEmpty(areaResult.getData())) {
            // 删除所有区域
            officeDao.delArea();
            // 批量插入所有区域信息
            officeDao.addArea(areaResult.getData());
            // 查询区域数量
            Integer count = officeDao.getAreaCount();
            if (count <= 0) {
                ExceptionLogger.error("OfficeServiceImpl#syncArea", "同步联合奖惩区域信息异常！未能成功插入数据！");
                throw new ServiceException("同步联合奖惩区域信息异常！新增区域数据异常！");
            }
        } else {
            ExceptionLogger.error("OfficeServiceImpl#syncArea", "同步联合奖惩区域信息异常！返回结果无区域信息数据!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncDept() {
        // 远程获取所有部门信息
        RestServiceResult<List<SysOfficeEntity>> deptResult = remoteBaseService.getOfficesByAreaId(null, null);
        if (null != deptResult && RestServiceResult.CODE_SUCCESS == deptResult.getCode()
                && !CollectionUtils.isEmpty(deptResult.getData())) {
            // 删除所有部门
            officeDao.delDept();
            // 批量插入所有部门信息
            officeDao.addDept(deptResult.getData());
            // 查询部门数量
            Integer count = officeDao.getDeptCount();
            if (count <= 0) {
                ExceptionLogger.error("OfficeServiceImpl#syncDept", "同步联合奖惩部门信息异常！未能成功插入数据！");
                throw new ServiceException("同步联合奖惩部门信息异常！新增部门数据异常！");
            }
        } else {
            ExceptionLogger.error("OfficeServiceImpl#syncDept", "同步联合奖惩部门信息异常！返回结果无部门信息数据!");
        }
    }

    @Override
    public List<Long> selectTopAndDownwardId(long id) {
        return officeDao.selectTopAndDownwardId(id);
    }

    @Override
    public List<Long> selectAllTopDeptId() {
        return officeDao.selectAllTopDeptId();
    }
}
